package com.luyu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luyu.entity.Client;
import com.luyu.mapper.ClientMapper;
import org.springframework.context.annotation.Primary;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
import org.springframework.security.oauth2.server.authorization.settings.OAuth2TokenFormat;
import org.springframework.security.oauth2.server.authorization.settings.TokenSettings;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
* @author HASEE
* @description 针对表【client】的数据库操作Service实现
* @createDate 2024-08-17 20:45:05
*/
@Primary
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements RegisteredClientRepository {

    private static Map<String,RegisteredClient> dataMap=new HashMap<>();
    {
        String clientId = "defaultClient";
        RegisteredClient registeredClient = RegisteredClient
                .withId(clientId)
                .clientId(clientId)
                .authorizationGrantType(AuthorizationGrantType.JWT_BEARER)
                .redirectUri("localhost:9090")
                .clientAuthenticationMethod(ClientAuthenticationMethod.NONE)
                .tokenSettings(TokenSettings.builder()
                        .accessTokenFormat(OAuth2TokenFormat.SELF_CONTAINED)
                        // .accessTokenFormat(OAuth2TokenFormat.REFERENCE)
                        .accessTokenTimeToLive(Duration.ofMinutes(500))
                        .build())
                .build();
        String msgRegisteredClientName="messaging-client";
        RegisteredClient msgRegisteredClient = RegisteredClient
                .withId(msgRegisteredClientName)
                .clientId(msgRegisteredClientName)
                .authorizationGrantType(AuthorizationGrantType.JWT_BEARER)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .redirectUri("https://www.baidu.com/REDIRECT_URI")
                .clientAuthenticationMethod(ClientAuthenticationMethod.NONE)
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .tokenSettings(TokenSettings.builder()
                        .accessTokenFormat(OAuth2TokenFormat.SELF_CONTAINED)
                        // .accessTokenFormat(OAuth2TokenFormat.REFERENCE)
                        .accessTokenTimeToLive(Duration.ofMinutes(500))
                        .build())
                .build();
        dataMap.put(clientId,registeredClient);
        dataMap.put(msgRegisteredClientName,msgRegisteredClient);
    }


    @Override
    public void save(RegisteredClient registeredClient) {
        dataMap.put(registeredClient.getClientId(),registeredClient);
    }

    @Override
    public RegisteredClient findById(String id) {
        return dataMap.get(id);
    }

    @Override
    public RegisteredClient findByClientId(String clientId) {
        return dataMap.get(clientId);
    }
}




